#include "gsystem.h"
#include "gui.h"


void lalala() {
	wchar_t buf[128];
	wsprintf(buf, L"%s\n", (GUI::GUIManager.findGUI(GetFocus()) ? GUI::GUIManager.findGUI(GetFocus())->getClassName() : L"QQ"));
	OutputDebugString(buf);
}


int GMainEventLoop() {
	MSG msg;
	GUI* gui;

	// main event loop
	while (GetMessage(&msg, NULL, 0, 0)) {

		// process dialog intended messages, say TAB stop support
		gui = GUI::GUIManager.findGUI(msg.hwnd);
		if (gui != NULL) {
			if (gui->getParent() != NULL) {
				if (IsDialogMessage(GetAncestor(msg.hwnd, GA_ROOTOWNER), &msg)) {
//					lalala();
					continue;
				}
			}
			else {
				if (IsDialogMessage(msg.hwnd, &msg)) {
//					lalala();
					continue;
				}
			}
		}

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return (int)msg.wParam;
}


LRESULT CALLBACK GWinProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	GUI* gui = GUI::GUIManager.findGUI(hWnd);
	bool handled = false;

	if (gui != NULL) {

		// check for destroy event of the main window
		if (uMsg == WM_DESTROY && gui == GUI::GUIManager.getMainWindow()) {
			PostQuitMessage(0);
			return 0;
		}

		handled = gui->GUIProc(uMsg, wParam, lParam);
	}

	if (handled)
		return 0;

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

